<?php

namespace App\Http\Controllers\VideoApi;

use App\Http\Config\ErrorCode;
use App\Library\UpTools;
use App\Models\video\LevelModel;
use App\Models\video\OrderModel;
use App\Models\video\ServiceModel;
use App\Models\video\VideoSettingModel;
use App\Models\video\VideoUserModel;
use Illuminate\Http\Request;

class LevelController extends BaseController
{
    public function __construct(Request $request)
    {
        $this->noCheckTokenAction = ['getMemberInfo', 'getList', 'getAllService'];
        parent::__construct($request);
    }

    public function getList(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->return_result($this->returnData);
        }
        $data = LevelModel::query()->where(["mall_id" => $request->mallData["id"], "status" => 1])
            ->select("id","name","pay_money","use_day","service","tag","original_price","desc")
            ->orderBy("sort", "desc")->get()->toArray();
        $data = UpTools::transferFields($data);
        $serviceData = ServiceModel::query()->where("mall_id", $request->mallData["id"])->select("id")->orderBy("sort", "desc")->get()->toArray();
        foreach ($data as &$item){
            if(empty($item["service"])){
                $item["service"] = [];
            }else{
                $service = explode(",", $item["service"]);
                $item["service"] = [];
                foreach ($serviceData as $datum){
                    $item["service"][] = ["id" => $datum["id"], "status" => in_array($datum["id"], $service) ? 1 : 0];
                }
            }
        }
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    public function getAllService(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->return_result($this->returnData);
        }
        $data = ServiceModel::query()->where("mall_id", $request->mallData["id"])
            ->select("name", "icon_id", "id")
            ->orderBy("sort", "desc")
            ->get()->toArray();
        $this->returnData["data"] = ServiceModel::transferField($data);
        return $this->return_result($this->returnData);
    }

    public function getMemberInfo(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->return_result($this->returnData);
        }
        $data = VideoSettingModel::getFields("protocol_title,protocol_content", ["mall_id" => $request->mallData["id"]], true, false);
        if(empty($data["protocolTitle"])){
            $data["protocolTitle"] = '';
        }
        if(empty($data["protocolContent"])){
            $data["protocolContent"] = '';
        }
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    public function toBecome(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        if(!$request->id){
            return ErrorCode::code("params_not_exist", '等级ID');
        }
        $levelData = LevelModel::getFields("pay_money", ["id" => $request->id], true, false);
        if(!empty($levelData)){
            if($levelData["payMoney"] > 0){
                if(!OrderModel::getCount(["user_id" => $request->userLoginData["id"], "belong_id" => $request->id, "type" => 3, "status" => 2])){
                    return ErrorCode::code("customized", '您未付费');
                }
            }
            if(VideoUserModel::getCount(["id" => $request->userLoginData["id"], "level_id" => $request->id])){
                return ErrorCode::code("customized", '您已经是该会员了');
            }
            $levelTime = time() + ($levelData["useDay"] * 86400);
            VideoUserModel::doUpdate(["level_id" => $request->id, "level_time" => $levelTime], ["id" => $request->userLoginData["id"]]);
        }
        return $this->return_result($this->returnData);
    }
}
